const config = require('../../common/config');
const util = require('../../utils/util');
const app = getApp();

//operation:0新增 1:查看/更新 2:删除
var operation = '';
var venicleId = '';
Page({
    data: {
        isDisabled: true,
        plateNum: '',
        plateNumEmpty: false,
        brand: '',
        brandEmpty: false,
        model: '',
        level: '',
        date: '',
        power: '',
        frameNum: '',
        realName: '',
        canEdit:true,
        showDelBtn:false
    },
    onLoad: function (opt) {
        wx.setNavigationBarTitle({
            title: '登记车辆'
        })
        var that = this;
        venicleId = opt.venicleId;
        if (venicleId == undefined) {
            operation = '0';
        } else {
            wx.showLoading({
                title: '加载中',
                icon: 'loading',
                duration: config.TIME_OUT,
                mask: true
            })

            wx.request({
                url: config.COMMPT_REQPATH,
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                method: 'POST',
                data: {
                    accessToken: '',
                    refreshToken: '',
                    serviceName: 'venicleService',
                    methodName: 'getMemberVenicleById',
                    reqData: JSON.stringify({
                        venicleId: venicleId,
                        memberId: app.globalData.userInfo.memberId
                    })
                },
                success: function (res) {
                    var resultData = res.data;
                    if (resultData.code == 1) {
                        resultData=resultData.data.venicleInfo;
                        operation = '1';
                        that.setData({
                            plateNum: resultData.plateNum,
                            brand: resultData.brand,
                            model: resultData.venicleModel,
                            level: resultData.venicleLevel,
                            date: resultData.venicleYear,
                            power: resultData.powerType,
                            frameNum: resultData.venicleFrame,
                            realName: resultData.realUser,
                            canEdit:false,
                            isDisabled:false,
                            showDelBtn:true
                        })

                    } else {
                        util.debug(resultData)
                    }
                },
                complete: function () {
                    wx.hideLoading();
                }
            })
        }
    },
    submitData: function (e) {
        var plateNum = this.data.plateNum;
        var brand = this.data.brand;
        var model = this.data.model;
        var level = this.data.level;
        var date = this.data.date;
        var power = this.data.power;
        var frameNum = this.data.frameNum;
        var realName = this.data.realName;
        if (plateNum.trim() == '' || brand.trim() == '') {
            return;
        }

        wx.showLoading({
            title: '加载中',
            icon: 'loading',
            duration: config.TIME_OUT,
            mask: true
        })

        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: 'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'venicleService',
                methodName: 'setMemberVenicle',
                reqData: JSON.stringify({
                    plateNum: plateNum,
                    brand: brand,
                    venicleModel: model,
                    venicleLevel: level,
                    venicleYear: date,
                    powerType: power,
                    venicleFrame: frameNum,
                    realUser: realName,
                    memberId: app.globalData.userInfo.memberId,
                    venicleId:venicleId,
                    operation:operation
                })
            },
            success: function (res) {
                var resultData = res.data;
                if (resultData.code == 1) {
                    wx.navigateBack({
                        delta: 2
                    })
                } else {
                    util.debug(resultData)
                }
            },
            complete: function () {
                wx.hideLoading();
            }
        })
    },
    changePlateNumInput: function (e) {
        var plateNum = e.detail.value;
        var plateNumEmpty = true;
        if (plateNum.length > 0) {
            plateNumEmpty = false;
        }
        this.setData({
            plateNum: plateNum,
            plateNumEmpty: plateNumEmpty
        })

        if (this.data.plateNumEmpty || this.data.brandEmpty) {
            this.setData({
                isDisabled: true
            })
        } else {
            this.setData({
                isDisabled: false
            })
        }
    },
    changeBrandInput: function (e) {
        var brand = e.detail.value;
        var brandEmpty = true;
        if (brand.length > 0) {
            brandEmpty = false;
        }
        this.setData({
            brand: brand,
            brandEmpty: brandEmpty
        })
        if (this.data.brandEmpty || this.data.plateNumEmpty) {
            this.setData({
                isDisabled: true
            })
        } else {
            this.setData({
                isDisabled: false
            })
        }
    },
    changeModelInput: function (e) {
        this.setData({
            model: e.detail.value
        })
    },
    changeLevelInput: function (e) {
        this.setData({
            level: e.detail.value
        })
    },
    bindDateChange: function (e) {
        this.setData({
            date: e.detail.value
        })
    },
    changePowerInput: function (e) {
        this.setData({
            power: e.detail.value
        })
    },
    changeFrameNumInput: function (e) {
        this.setData({
            frameNum: e.detail.value
        })
    },
    changeRealNameInput: function (e) {
        this.setData({
            realName: e.detail.value
        })
    },
    deleteData:function (e) {
        wx.showLoading({
            title: '加载中',
            icon: 'loading',
            duration: config.TIME_OUT,
            mask: true
        })

        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: 'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'venicleService',
                methodName: 'setMemberVenicle',
                reqData: JSON.stringify({
                    memberId: app.globalData.userInfo.memberId,
                    venicleId:venicleId,
                    operation:'2'
                })
            },
            success: function (res) {
                var resultData = res.data;
                if (resultData.code == 1) {
                    wx.navigateBack({
                        delta: 2
                    })
                } else {
                    util.debug(resultData)
                }
            },
            complete: function () {
                wx.hideLoading();
            }
        })
    }
})